package com.bwie.service.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.*;
import com.bwie.pojo.*;
import com.bwie.service.ServiceItemService;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.bwie.vo.PageVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【service_item】的数据库操作Service实现
* @createDate 2025-07-11 11:03:54
*/
@Service
public class ServiceItemServiceImpl extends ServiceImpl<ServiceItemMapper, ServiceItem>
    implements ServiceItemService{

    @Autowired
    ServiceItemMapper serviceItemMapper;
    @Autowired
    ServiceTypeMapper serviceTypeMapper;
    @Autowired
    FastFileStorageClient fastFileStorageClient;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    OrdersMapper orderMapper;
    @Autowired
    CustomerAddressMapper customerAddressMapper;

    @Override
    public R getServiceItemList(PageVo pageVo) {
        Page<ServiceItem> serviceItemPage = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());

        Page<ServiceItem> itemPage = serviceItemMapper.getServiceItemList(serviceItemPage, pageVo);
        return R.OK(itemPage);
    }

    @Override
    public R addServiceItem(ServiceItem serviceItem) {
        ServiceItem item = serviceItemMapper.selectOne(new QueryWrapper<ServiceItem>().lambda().eq(ServiceItem::getName, serviceItem.getName()));
        if(item!=null){
            return R.ERROR(500,"名称已存在");
        }
        String code = generateOrderNo();
        serviceItem.setCode(code);
        //生成服务编号
        serviceItemMapper.insert(serviceItem);
        return R.OK();
    }
    // 生成订单号
    private String generateOrderNo() {
        /*
         * 订单号生成规则:
         * 前6位:年月日(YYMMDD)
         * 后13位:Redis自增序列
         */
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        String datePart = sdf.format(new Date());

        // Redis自增获取序列号
        String key = "order:seq:" + datePart;
        Long seq = redisTemplate.opsForValue().increment(key, 1);

        return datePart + String.format("%013d", seq);
    }
    @Override
    public R getServiceType() {
        List<ServiceType> serviceTypes = serviceTypeMapper.selectList(null);
        return R.OK(serviceTypes);
    }

    @Override
    public R uploadFile(MultipartFile file) {

        try {
            String extName = FileUtil.extName(file.getOriginalFilename());
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url = "http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
            e.printStackTrace();
            return R.ERROR(500,"上传失败");
        }
    }

    @Override
    public R getCouponList(PageInfoVo pageInfoVo) {
        Page<Coupon> couponPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        Page<Coupon> page = couponMapper.selectPage(couponPage, null);
        return R.OK(page);
    }

    @Override
    public R updateTypeStatus(ServiceType serviceType) {
        serviceType.setStatus(1);
        serviceTypeMapper.updateById(serviceType);
        return R.OK();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R makeOrder(Orders order) {

        //添加地址表
        CustomerAddress customerAddress = new CustomerAddress();
        customerAddress.setCustomerId(1L);
        customerAddress.setContactName("未知");
        customerAddress.setContactPhone("18835795973");
        customerAddress.setAddress(order.getAddress());
        customerAddress.setUpdateTime(new Date());
        customerAddressMapper.insert(customerAddress);
        //添加订单表
        ServiceItem serviceItem = serviceItemMapper.selectById(order.getId());
        Orders order1 = new Orders();
        order1.setOrderNo(serviceItem.getCode());
        order1.setAddressId(customerAddress.getId());
        order1.setAmount(serviceItem.getPrice());
        order1.setStatus(0);
        order1.setCustomerId(1L);
        order1.setServiceItemId(serviceItem.getId());
        order1.setServiceTime(order.getServiceTime());
        order1.setActualAmount(serviceItem.getPrice());
        orderMapper.insert(order1);


        return R.OK();
    }

    @Override
    public void updatePayStatus(String out_trade_no) {
        serviceItemMapper.updatePayStatus(out_trade_no);
    }


}




